<template>
  <div class="main-all">
    <p><i class="el-icon-s-check"></i>教师</p>
    <div class="main">
      <div class="main-1">
        <el-button icon="el-icon-plus" @click="dialogFormVisible = true; ADD()">新建</el-button>
        <div style="display: flex;">
          <el-input style="margin-right: 20px; height: 30px; width: 100px;" v-model="inputName"
            placeholder="教师"></el-input>
          <el-input style="margin-right: 20px; height: 30px; width: 100px;" v-model="inputMajor"
            placeholder="系名"></el-input>
          <el-button type="primary" @click="getPage" icon="el-icon-search">搜索</el-button>
        </div>
      </div>
      <el-pagination @current-change="fn2" :current-change="page" background layout="prev, pager, next" :total="total"
        style="margin-bottom: 10px;">
        <!--                                          total页码，设定为变量 -->
      </el-pagination>
      <div style="overflow-y: scroll;width: 100%;height: 80%;">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" label="教师ID">
        </el-table-column>
        <el-table-column prop="number" label="教师工号">
        </el-table-column>
        <el-table-column prop="name" label="教师姓名">
        </el-table-column>
        <el-table-column prop="departmentName" label="所属系">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="dialogFormVisible = true; changed(scope.row)">编辑</el-button>
            <el-button type="text" @click=" deletes(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      </div>
      <!--  -->


      <el-dialog width="35%" title="编辑" :visible.sync="dialogFormVisible">
        <el-form :model="form" :label-width="formLabelWidth" :rules="rules">
          <el-form-item label="教师工号" prop="number">
            <el-input v-model="form.number" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="教师姓名" prop="name">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="所属系" prop="department">
            <el-select style="width: 100%;" v-model="form.department" placeholder="请选择系">
              <el-option v-for="(item, index) in tableDataAll" :key="index" :label="item.name"
                :value="item.name"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="登录密码" prop="password">
            <el-input v-model="form.password" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogFormVisible = false; ADDorChange()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>


</template>

<script>
import serve from '../api/index.js'
import { getPages, getList, ADDList, changeorno, deleted, getListAll, getTeacher } from '../api/teacher.js'
export default {
  data() {
    return {
      inputName: '',
      inputMajor: '',
      // 搜索二人组
      total: 100,
      // 页码算数
      page: 1,
      // 页数
      tableData: [],
      // 老师管理的res回复
      tableDataAll: [],
      // 系管理的res回复
      add: '',
      dialogFormVisible: false,
      form: {
        name: '',       //教师姓名
        number: '',     //教师工号
        department: '',      //所属系
        departmentId: '',    //所属系ID
        password: '',    //登录密码
        ID: ''           //ID
      },
      formLabelWidth: '80px',
      rules: {
        number: [
          { required: true, message: '请输入教师工号', trigger: 'blur' },
          { min: 12, max: 12, message: '长度为12个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度为6个字符以上', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入教师姓名', trigger: 'blur' },
        ],
        //  department: [
        //   { required: true, message: '请输入系', trigger: 'blur' },
        // ],
      }
    }
  },
  methods: {
    getPage() {
      getPages(this.inputMajor, this.inputName).then((res) => {
        console.log(res.data);
        this.total = res.data * 10
        if (res.message == 'success') {
          getList(this.page, this.inputMajor  , this.inputName).then((res) => {
            console.log(res);
            this.tableData = res.data
            console.log(this.tableData, 'getList');

          })
          getListAll().then((res) => {
            this.tableDataAll = res.data
            console.log(this.tableDataAll, 'getListAll');
          })
        }
      })
    },//获取列表数据
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => { });
    },//element
    fn2(val) {
      this.page = `${val}`
      console.log(this.page);
      this.getPage()
    },//获取页数


    fn() {
      // 获取 系ID--departmentId
      this.tableDataAll.filter((item) => {
        if (item.name == this.form.department) {
          this.form.departmentId = item.id
          // console.log(this.form.departmentId, 'departmentId');
        }
      })
    },
    ADD() {
      this.fn()
      this.OR = 'ADD'
      console.log(this.form);
    },//添加列表数据  

    changed(e) {
      this.OR = 'changed'
      console.log(e, 'e');
      this.form.department = e.departmentName,
        this.form.ID = e.id,
        this.form.number = e.number
      this.form.name = e.name
      this.fn()
      getTeacher(this.page, e.number, e.name, this.form.departmentId, e.password, e.id).then(res => {
        console.log(res, 'getTeacher');
        this.form.departmentId = res.data.departmentId
      })

    }, //修改列表数据

    ADDorChange() {
      if (this.OR == 'ADD') {
        this.fn()
        ADDList(this.form.number, this.form.name, this.form.departmentId, this.form.password,).then((res) => {
          console.log(res);
          this.getPage()
        })
      } else if (this.OR == 'changed') {
        this.fn()
        changeorno(this.form.departmentId, this.form.ID, this.form.name, this.form.number, this.form.password).then((res) => {
          console.log(res);
          this.getPage()
        })
      }
    },





    deletes(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleted(id).then((res) => {
          console.log(res);
        })
        this.getPage()
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      }); //删除列表数据   xxxx未完成

    }

  },
  mounted() {
    this.getPage()  //初始化列表数据
  }
}

</script>

<style scoped>
.el-table {
  width: 100% !important;
}

.main {
  .el-button {
    height: 40px;
  }

  .main-1 {
    height: 60px;
    display: flex;
    align-items: center;
    padding: 10px;
    justify-content: space-between;
  }


}
</style>
